﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZMCT.UI.PopWindows
{
    // <summary>
    /// 选择字段页面
    /// </summary>
    /// Title:SelectField
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2012-06-07  高强   [创建]
    ///  
    public partial class SelectField : EFViewCmes
    {
        private EFDataField contentField;

        public SelectField(EFDataField field)
        {
            contentField = field;
            InitializeComponent();
        }

        /// <summary>
        /// 确定按钮按下增加隐患项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btnSelectConfirm(object sender, RoutedEventArgs e)
        {
            string returnValue = "";
            List<EIRow> rowTemp = grid.GetCheckedEIRows();

            if (rowTemp.Count == 0)
            {
                ValidateMessagePopup("请选择需要添加的字段！");
            }
            else
            {
                bool firstflag = true;
                foreach (EIRow row in rowTemp)
                {
                    if (!firstflag)
                    {
                        returnValue += " " + row["TnameValue"];
                    }
                    else
                    {
                        returnValue += row["TnameValue"];
                        firstflag = false;
                    }
                }

                TextBox box = contentField.Content as TextBox;
                int selectStart = box.SelectionStart;
                int selectLength = box.SelectionLength;

                string old_value = contentField.Value.ToString();
                if (selectLength == 0)
                {
                    contentField.Value = old_value.Substring(0, selectStart) + returnValue 
                                            + old_value.Substring(selectStart, old_value.Length-selectStart);
                }
                else
                {
                    contentField.Value = old_value.Substring(0, selectStart) + returnValue 
                                            + old_value.Substring((selectStart + selectLength), 
                                                            (old_value.Length - (selectStart + selectLength)));
                }
            }

            EFWindow wnd = Parent as EFWindow;
            wnd.DialogResult = true;
            wnd.Close();
        }

        /// <summary>
        /// 返回按钮按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btnSelectCancle(object sender, RoutedEventArgs e)
        {
            EFWindow wnd = Parent as EFWindow;
            wnd.DialogResult = false;
            wnd.Close();
        }

        /// <summary>
        /// 提示错误消息
        /// </summary>
        /// <param name="str"></param>
        private void ValidateMessagePopup(string str)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(str);
            pop.Show(6);

            return;
        }
    }
}
